/**
 * @description mongoose 连接数据库
 * @author llj
 */
const mongoose = require('mongoose')
const url = 'mongodb://localhost:27017' //本地默认的mongodb服务地址
const dbName = 'testdb' //数据库名称

mongoose.set('useCreateIndex', true)
mongoose.set('useFindAndModify', false)

//开始连接
mongoose.connect(`${url}/${dbName}`, {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
//连接对象
const db = mongoose.connection
db.on('error', err => {
  console.log('数据库连接失败', err);
})
db.once('open', () => {
  //用以测试数据库连接是否成功
  console.log('数据库连接成功');
})
module.exports = mongoose
